package com.example.book.service.impl;

import com.example.book.dao.BookDao;
import com.example.book.dao.impl.BookDaoImpl;
import com.example.book.pojo.Book;
import com.example.book.pojo.Page;
import com.example.book.service.BookService;

import java.util.List;

/**
 * @Description
 * @ClassName BookServiceImpl
 * @Author 山山大魔王
 * @Date 2021/11/18 17:05
 * @Version 1.0
 */
public class BookServiceImpl implements BookService {
    private BookDao bookDao = new  BookDaoImpl();
    @Override
    public void addBook(Book book) {
         bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBook() {
        return bookDao.queryBook();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();


        //设置每页数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal = pageTotalCount%pageSize==0 ? pageTotalCount/pageSize : pageTotalCount/pageSize+1;



        //设置总页码
        page.setPageTotal(pageTotal);

        //设置当前页
        page.setPageNo(pageNo);


        //求当前页数据的开始索引
        int begin = (page.getPageNo() - 1)*pageSize;
        //求当前页数据
        List<Book> items = bookDao.queryForPageItems(begin,pageSize);
        //设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize,int max,int min) {
        Page<Book> page = new Page<>();


        //设置每页数量
        page.setPageSize(pageSize);
        //求总记录数
        Integer pageTotalCount = bookDao.queryForPageTotalCountByPrice(min,max);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal = pageTotalCount%pageSize==0 ? pageTotalCount/pageSize : pageTotalCount/pageSize+1;



        //设置总页码
        page.setPageTotal(pageTotal);

        //设置当前页
        page.setPageNo(pageNo);


        //求当前页数据的开始索引
        int begin = (page.getPageNo() - 1)*pageSize;
        //求当前页数据
        List<Book> items = bookDao.queryForPageItemsByPrice(begin,pageSize,min,max);
        //设置当前页数据
        page.setItems(items);
        return page;
    }
}
